/**
 * Copyright (c) 2017-2017 All Rights Reserved.
 */
package com.zbest.zpay.gw.assemble.wx;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.stereotype.Service;

import com.zbest.common.lang.util.RandomStringUtil;
import com.zbest.zpay.gw.dal.model.GwChannelPayConfigDO;
import com.zbest.zpay.gw.model.MessageContext;
import com.zbest.zpay.masterdata.dal.model.ChannelConfigDO;
import com.zbest.zpay.service.gw.model.GwKeyEnum;

/**
 * 
 *
 * @author Administrator
 * @version $Id: UnifiedorderAssemble.java, v 0.1 2017-5-2 下午11:18:14 Administrator Exp $
 */
@Service("unifiedorderAssemble")
public class UnifiedorderAssemble extends AbstractWxMessageAssemble {

    /** 
     * @see com.zbest.zpay.gw.assemble.wx.AbstractWxMessageAssemble#buildOriginParam(com.zbest.zpay.gw.model.MessageContext)
     */
    @Override
    protected Map<String, String> buildOriginParam(MessageContext context) {

        ChannelConfigDO channelConfigDO = channelConfigCache.get(context.getChannelId());
        GwChannelPayConfigDO channelPayConfigDO = channelPayConfigCache.get(channelConfigDO
            .getChannelSystemId());

        // 商户订单号，商户网站订单系统中唯一订单号，必填
        String instSerialNo = context.getRequestData(GwKeyEnum.INST_SERIAL_NO.getCode());
        // 订单名称，必填
        String subject = context.getRequestData(GwKeyEnum.GOODS_TITLE.getCode());
        // 付款金额，必填
        String total_fee = context.getRequestData(GwKeyEnum.TOTAL_AMOUNT.getCode());
        // 商品描述，可空
        String body = context.getRequestData(GwKeyEnum.GOODS_DESC.getCode());
        // 币种，不可空
        String currency = context.getRequestData(GwKeyEnum.CURRENCY.getCode());

        Map<String, String> params = new HashMap<String, String>();
        //微信分配的公众账号ID（企业号corpid即为此appId）
        params.put("appid", channelPayConfigDO.getAppId());
        //  微信支付分配的商户号
        params.put("mch_id", channelPayConfigDO.getPartnerId());
        params.put("nonce_str", RandomStringUtil.getRandomStringByLength(32));
        //商品或支付单简要描述
        params.put("body", subject);
        //商户系统内部订单号，要求32个字符内
        params.put("out_trade_no", instSerialNo);
        //符合ISO4217标准的三位字母代码详见
        params.put("fee_type", currency);
        //标价金额，单位为该币种最小计算单位，只能为整数
        params.put("total_fee", total_fee);
        //APP和网页支付提交用户端ip，Native支付填调用微信支付API的机器IP
        params.put("spbill_create_ip", body);
        //接收微信支付异步通知回调地址
        params.put("notify_url", channelPayConfigDO.getNotifyUrl());
        //取值如下：JSAPI，NATIVE，APP
        params.put("trade_type", currency);

        return params;
    }

    /** 
     * @see com.zbest.zpay.gw.assemble.wx.AbstractWxMessageAssemble#buildRequestMessage(com.zbest.zpay.gw.model.MessageContext, java.util.Map)
     */
    @Override
    protected String buildRequestMessage(MessageContext context, Map<String, String> data) {

        StringBuilder sb = new StringBuilder();
        sb.append("<xml>");
        for (Entry<String, String> entry : data.entrySet()) {
            sb.append("<").append(entry.getKey()).append(">").append(entry.getValue()).append("</")
                .append(entry.getKey()).append(">");
        }
        sb.append("</xml>");

        return sb.toString();
    }
}
